package com.wanmait.bootdemo.controller;

import com.wanmait.bootdemo.pojo.QuestionType;
import com.wanmait.bootdemo.service.QuestionTypeService;
import com.wanmait.bootdemo.util.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/manage/questionType")
public class QuestionTypeController {
    @Resource
    private QuestionTypeService questionTypeService;
    @RequestMapping("list")
    @ModelAttribute("questionTypes")
    public List<QuestionType> list()
    {
        return questionTypeService.findAll();
    }

    /*@ExceptionHandler(QuestionTypeNotFoundException.class)
    public ModelAndView process(QuestionTypeNotFoundException e)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("exception",e);
        return modelAndView;
    }*/

    @RequestMapping("textAjax")
    @ResponseBody
    public String testAjax() throws Exception {
        throw new Exception("hello error");
       // return "hello";
    }

    @RequestMapping("listAjax")
    @ResponseBody
    public JSONResult  listAjax() throws Exception {
        throw new Exception("hello error");
        /*List<QuestionType> questionTypes =  questionTypeService.findAll();
        return JSONResult.ok(questionTypes);*/
    }



    @RequestMapping("view")
    @ModelAttribute("questionType")
    public QuestionType view(Integer id)
    {
        return questionTypeService.findById(id);
    }


    @RequestMapping("viewAjax")
    @ResponseBody
    public JSONResult viewAjax(Integer id)
    {
       QuestionType questionType =  questionTypeService.findById(id);
       if(questionType!=null) {
           return JSONResult.ok(questionType);
       }
       else
       {
           return JSONResult.errorMsg("类型不存在");
       }
    }

}
